package com.spx.core.ui.properties;

import java.util.Set;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.dialogs.PropertyPage;

import com.spx.core.CoreActivator;
import com.spx.core.types.Activity;
import com.spx.core.types.Role;
import com.spx.core.types.User;
import com.spx.kernel.ui.widgets.DoubleListSelector;

public class RolePropertyPage extends PropertyPage
{
	private static final String NAME_TITLE = "Name:";
	private static final String EMAIL_TITLE = "Email:";
	private static final String ENABLED_TITLE = "Enabled:";
	private static final String LASTLOGIN_TITLE = "Last Login:";
	private static final String PASSWORD_TITLE = "Password:";
	private static final String PASSWORD_TITLE2 = "Repeat Password:";

	private static final int TEXT_FIELD_WIDTH = 50;

	private Text nameText;
	private Role role = new Role();
	private String id;
	private Text userNameText;
	private Text userEmailText;
	private Text userPasswordText;
	private Text userPassword2Text;
	private Button userEnabledCheckbox;

	Set<Activity> chosenActions;

	Set<Activity> allActions;

	private DoubleListSelector<Activity> selector;

	/**
	 * Constructor for SamplePropertyPage.
	 */
	public RolePropertyPage()
	{
		super();
	}

	private void addFirstSection(Composite composite)
	{

		final IAdaptable userAdaptor = getElement();
		role = (Role) userAdaptor.getAdapter(Role.class);
		id = role.getId();

		chosenActions = role.getActions();
		allActions = CoreActivator.getDefault().getSecurityService().fetchAllActions(0, Integer.MAX_VALUE);

		// Label for path field
		final Label nameLabel = new Label(composite, SWT.NONE);
		nameLabel.setText(NAME_TITLE);
		userNameText = new Text(composite, SWT.WRAP | SWT.BORDER);

		userNameText.setText(role.getName() + "");
		final GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.FILL;
		gridData.grabExcessHorizontalSpace = true;
		userNameText.setLayoutData(gridData);

		// Label for path field
		final Label enabledLabel = new Label(composite, SWT.NONE);
		enabledLabel.setText(ENABLED_TITLE);
		userEnabledCheckbox = new Button(composite, SWT.WRAP | SWT.CHECK);
		userEnabledCheckbox.setSelection(role.isOn());

		final GridData gridData2 = new GridData();
		gridData2.horizontalAlignment = GridData.FILL;
		gridData2.grabExcessHorizontalSpace = true;
		gridData2.horizontalSpan = 3;
		userNameText.setLayoutData(gridData);

		selector = new DoubleListSelector<Activity>(composite, SWT.NONE, allActions, chosenActions);
		selector.setLayoutData(gridData2);
	}

	/**
	 * @see PreferencePage#createContents(Composite)
	 */
	@Override
	protected Control createContents(Composite parent)
	{
		final Composite composite = createDefaultComposite(parent);
		addFirstSection(composite);

		return composite;
	}

	private Composite createDefaultComposite(Composite parent)
	{

		final Composite composite = new Composite(parent, SWT.NONE);
		final GridLayout layout = new GridLayout(2, true);
		composite.setLayout(layout);
		final GridData data = new GridData(GridData.FILL);
		data.grabExcessHorizontalSpace = true;
		composite.setLayoutData(data);

		return composite;
	}

	@Override
	protected void performDefaults()
	{

	}

	@Override
	public boolean performOk()
	{

		role.setName(userNameText.getText());

		role.setOn(userEnabledCheckbox.getSelection());
		role.getActions().clear();
		role.getActions().addAll(selector.getChosen());

		CoreActivator.getDefault().getSecurityService().modify(role);
		// store the value in the owner text field

		return true;
	}
	

	
}